<!-- 友链编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改区服配置' : '添加区服配置'"
    :visible="visible"
    width="750px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :close-on-click-modal="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="游戏:" prop="game_id">
        <PageSelect
          :placeholder="'请选择游戏'"
          v-model="form.game_id"
          :whereName="'name'"
          :url="'/box_game/gameCommonMenuList'"
          :optionLable="'game_name'"
          :optionvalue="'id'"
          :multiple="false"
        />
      </el-form-item>
      <el-form-item label="宣传视频:" prop="media">
        <gameUploadBanner :limit="1" v-model="form.media"></gameUploadBanner>
      </el-form-item>
      <el-form-item label="宣传图:" prop="icons">
        <uploadImage :limit="1" v-model="form.icons"></uploadImage>
      </el-form-item>

      <el-form-item label="区服类型:" prop="type">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option label="测试区服" :value="1" />
          <el-option label="正式区服" :value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="区服名称：" prop="name">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入区服名称"
        />
      </el-form-item>
      <el-form-item label="区服ID：" prop="server_id">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.server_id"
          placeholder="请输入区服ID"
        />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="开服时间:" prop="open_time">
        <el-date-picker
          v-model="form.open_time"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="区服介绍:" prop="desc">
        <el-input
          type="textarea"
          v-model="form.desc"
          placeholder="请输入区服介绍"
          clearable
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import PageSelect from "@/components/pageSelect/index.vue";
import uploadImage from "@/components/uploadImage.vue";
import gameUploadBanner from "@/components/gameUploadBanner.vue";

export default {
  name: "edit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: {
      type: [String, Array, Object, File, Blob],
    },
  },

  components: { gameUploadBanner, uploadImage, PageSelect },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        name: [{ required: true, message: "请输入区服名称", trigger: "blur" }],
        server_id: [
          { required: true, message: "请输入区服ID", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        type: [{ required: true, message: "请选择区服类型", trigger: "blur" }],
        game_id: [
          { required: true, message: "请输入游戏编码", trigger: "blur" },
        ],
        open_time: [
          { required: true, message: "请选择开服时间", trigger: "blur" },
        ],
        desc: [{ required: true, message: "请输入区服介绍", trigger: "blur" }],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  // watch: {
  //   data(time, format) {
  //     if (this.data) {
  //       this.form = Object.assign({}, this.data);
  //       this.form.open_time = this.$util.toDateString(
  //         this.form.open_time,
  //         format
  //       );
  //       console.log(this.form, "form");
  //       this.isUpdate = true;
  //     } else {
  //       this.form = {};
  //       this.isUpdate = false;
  //     }
  //   },
  // },
  created(time, format) {
    if (this.data) {
      this.form = Object.assign({}, this.data);
      this.form.open_time = this.$util.toDateString(
        this.form.open_time,
        format
      );
      // console.log(this.form, "form");
      this.isUpdate = true;
    } else {
      this.form = {};
      this.isUpdate = false;
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/BoxGameOpen/edit", this.form)
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          this.initForm();
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped></style>
